class Solution(object):
    def checkValid(self, matrix):
        n = len(matrix)
        for i in range(n):
            out = [0] * n
            for j in range(n):
                out[matrix[i][j] - 1] = 1
            if not all(out):
                return False
        for j in range(n):
            out = [0] * n
            for i in range(n):
                out[matrix[i][j] - 1] = 1
            if not all(out):
                return False
        return True
